import CryptoJs from 'crypto-js'
import { DES_KEY } from '../app/config.js'

/**
 * md5加密
 * @param {string} message
 */
export const md5 = (message) => {
    return CryptoJs.MD5(message).toString().toUpperCase()
}

/**
 * DES加密
 * @param {string} message
 */
const enDES = (message) => {
    const keyHex = CryptoJS.enc.Utf8.parse(DES_KEY)
    const encrypted = CryptoJS.DES.encrypt(message, keyHex, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7,
    })
    return encrypted.ciphertext.toString(CryptoJS.enc.Base64)
}

/**
 * DES解密
 * @param {string} ciphertext
 */
const deDES = (ciphertext) => {
    const keyHex = CryptoJS.enc.Utf8.parse(DES_KEY)
    const ciphertextBytes = CryptoJS.enc.Base64.parse(ciphertext)
    const options = { ciphertext: ciphertextBytes }
    const decrypted = CryptoJS.DES.decrypt(options, keyHex, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7,
    })
    return decrypted.toString(CryptoJS.enc.Utf8)
}
